package bb.lanxing.lib.widget;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.internal.view.SupportMenu;

import bb.lanxing.R;
import bb.lanxing.lib.widget.utils.Density;

public abstract class BaseScrollTabStrip extends HorizontalScrollView {
    private static final int[] ATTRS = {android.R.attr.textSize, android.R.attr.textColor};

    protected LinearLayout tabsContainer;


    protected int currentPosition;
    protected float currentPositionOffset;
    private LinearLayout.LayoutParams defaultTabLayoutParams;
    private int dividerColor;
    private int dividerPadding;
    private Paint dividerPaint;
    private boolean fillTabBlank;
    protected int[] icons;
    private int indicatorColor;
    private int indicatorHeight;
    private int lastScrollX;
    private Paint rectPaint;
    private Paint redDot;
    protected boolean[] redDotIndex;
    private int scrollOffset;
    private boolean showDivider;
    protected TabClickListener tabClickListener;
    protected int tabCount;
    private int tabPadding;
    private int tabTextColor;
    private int tabTextSize;

    private boolean textBold;
    protected String[] titles;
    private int underlineColor;
    private int underlineHeight;

    public interface TabClickListener {
        void onTabClick(int i);
    }

    protected abstract void onTabClick(int i);

    public BaseScrollTabStrip(Context context) {
        this(context, null);
    }

    public BaseScrollTabStrip(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public BaseScrollTabStrip(Context context, AttributeSet attributeSet, int defStyleAttr) {
        super(context, attributeSet, defStyleAttr);
        currentPosition = 0;
        currentPositionOffset = 0.0f;
        tabTextSize = 16;
        tabTextColor = 0xFF666666;
        indicatorColor = 0xFF666666;
        underlineColor = 0x1A000000;
        dividerColor = 0x1A000000;
        scrollOffset = 52;
        indicatorHeight = 3;
        underlineHeight = 1;
        dividerPadding = 12;
        tabPadding = 24;
        fillTabBlank = true;
        textBold = false;
        lastScrollX = 0;
        init(context, attributeSet, defStyleAttr, 0);
    }

    public BaseScrollTabStrip(Context context, AttributeSet attributeSet, int defStyleAttr, int i2) {
        super(context, attributeSet, defStyleAttr, i2);
        currentPosition = 0;
        currentPositionOffset = 0.0f;
        tabTextSize = 16;
        tabTextColor = 0xFF666666;
        indicatorColor = 0xFF666666;
        underlineColor = 0x1A000000;
        dividerColor = 0x1A000000;
        scrollOffset = 52;
        indicatorHeight = 3;
        underlineHeight = 1;
        dividerPadding = 12;
        tabPadding = 24;
        fillTabBlank = true;
        textBold = false;
        lastScrollX = 0;
        init(context, attributeSet, defStyleAttr, 0);
    }

    @SuppressLint("RestrictedApi")
    private void init(Context context, AttributeSet attributeSet, int i, int i2) {
        setFillViewport(true);
        setWillNotDraw(false);

        tabsContainer = new LinearLayout(context);
        tabsContainer.setOrientation(LinearLayout.HORIZONTAL);
        tabsContainer.setLayoutParams(new FrameLayout.LayoutParams(-1, -1));
        addView(tabsContainer);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        scrollOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, scrollOffset, displayMetrics);
        indicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, displayMetrics);
        underlineHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, underlineHeight, displayMetrics);
        dividerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerPadding, displayMetrics);
        tabPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tabPadding, displayMetrics);
        tabTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tabTextSize, displayMetrics);
        int applyDimension = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, displayMetrics);
        initAttr(context, attributeSet, i, i2);
        rectPaint = new Paint();
        rectPaint.setAntiAlias(true);
        rectPaint.setStyle(Paint.Style.FILL);
        if (showDivider) {
            dividerPaint = new Paint();
            dividerPaint.setAntiAlias(true);
            dividerPaint.setStrokeWidth(applyDimension);
        }
        this.defaultTabLayoutParams = new LinearLayout.LayoutParams(-2, -1);
        Paint paint3 = new Paint();
        this.redDot = paint3;
        paint3.setColor(SupportMenu.CATEGORY_MASK);
        redDot.setStyle(Paint.Style.FILL);
    }

    private void initAttr(Context context, AttributeSet attributeSet, int i, int i2) {
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, ATTRS);
        tabTextSize = obtainStyledAttributes.getDimensionPixelSize(0, tabTextSize);
        tabTextColor = obtainStyledAttributes.getColor(1, tabTextColor);
        obtainStyledAttributes.recycle();
        TypedArray obtainStyledAttributes2 = context.obtainStyledAttributes(attributeSet, R.styleable.ScrollTabStrip, i, i2);
        indicatorColor = obtainStyledAttributes2.getColor(R.styleable.ScrollTabStrip_stbIndicatorColor, indicatorColor);
        tabTextColor = obtainStyledAttributes2.getColor(R.styleable.ScrollTabStrip_stbColor, tabTextColor);
        underlineColor = obtainStyledAttributes2.getColor(R.styleable.ScrollTabStrip_stbUnderlineColor, underlineColor);
        dividerColor = obtainStyledAttributes2.getColor(R.styleable.ScrollTabStrip_stbDividerColor, dividerColor);
        scrollOffset = obtainStyledAttributes2.getDimensionPixelSize(R.styleable.ScrollTabStrip_stbScrollOffset, scrollOffset);
        indicatorHeight = obtainStyledAttributes2.getDimensionPixelSize(R.styleable.ScrollTabStrip_stbIndicatorHeight, indicatorHeight);
        underlineHeight = obtainStyledAttributes2.getDimensionPixelSize(R.styleable.ScrollTabStrip_stbUnderlineHeight, underlineHeight);
        showDivider = obtainStyledAttributes2.getBoolean(R.styleable.ScrollTabStrip_stbShowDivider, true);
        fillTabBlank = obtainStyledAttributes2.getBoolean(R.styleable.ScrollTabStrip_stbFillTabBlank, true);
        tabPadding = obtainStyledAttributes2.getDimensionPixelSize(R.styleable.ScrollTabStrip_stbTabPadding, tabPadding);
        textBold = obtainStyledAttributes2.getBoolean(R.styleable.ScrollTabStrip_stbTextBold, false);
        obtainStyledAttributes2.recycle();
    }

    public void notifyAllTabs() {
        tabsContainer.removeAllViews();
        String[] strArr = titles;
        int i = 0;
        if (strArr != null) {
            tabCount = strArr.length;
            while (i < this.tabCount) {
                Drawable drawable = null;
                int[] iArr = this.icons;
                if (iArr != null && i < iArr.length && iArr[i] != 0) {
                    drawable = getResources().getDrawable(this.icons[i]);
                }
                addTextTab(i, this.titles[i], drawable);
                i++;
            }
        } else {
            int[] iArr2 = this.icons;
            if (iArr2 != null) {
                this.tabCount = iArr2.length;
                while (true) {
                    int[] iArr3 = this.icons;
                    if (i >= iArr3.length) {
                        break;
                    }
                    addIconTab(i, iArr3[i]);
                    i++;
                }
            }
        }
        if (this.fillTabBlank) {
            post(new Runnable() {
                @Override
                public void run() {
                    int width = BaseScrollTabStrip.this.getWidth() / BaseScrollTabStrip.this.tabCount;
                    for (int i2 = 0; i2 < BaseScrollTabStrip.this.tabCount; i2++) {
                        View childAt = BaseScrollTabStrip.this.tabsContainer.getChildAt(i2);
                        ViewGroup.LayoutParams layoutParams = childAt.getLayoutParams();
                        layoutParams.width = width;
                        childAt.setLayoutParams(layoutParams);
                    }
                }
            });
        }
    }

    private BaseScrollTabStrip addTextTab(int i, String str, Drawable drawable) {
        TextView textView = new TextView(getContext());
        textView.setText(str);
        textView.setGravity(Gravity.CENTER);
        textView.setSingleLine();
        textView.getPaint().setFakeBoldText(this.textBold);
        textView.setTextSize(0, this.tabTextSize);
        if (this.currentPosition == i) {
            textView.setTextColor(this.indicatorColor);
        } else {
            textView.setTextColor(this.tabTextColor);
        }
        if (drawable != null) {
            int i2 = this.tabTextSize;
            drawable.setBounds(0, 0, i2, i2);
            SpannableString spannableString = new SpannableString(" " + str);
            spannableString.setSpan(new CenterAlignImageSpan(drawable), 0, 1, 17);
            textView.setText(spannableString);
        }
        addTab(i, textView);
        return this;
    }

    private BaseScrollTabStrip addIconTab(int i, int i2) {
        ImageButton imageButton = new ImageButton(getContext());
        imageButton.setImageResource(i2);
        addTab(i, imageButton);
        return this;
    }

    private void addTab(final int i, View view) {
        view.setFocusable(true);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view2) {
                BaseScrollTabStrip baseScrollTabStrip = BaseScrollTabStrip.this;
                baseScrollTabStrip.switchTabTextColor(baseScrollTabStrip.currentPosition, i);
                BaseScrollTabStrip.this.scrollToChild(i, 0);
                BaseScrollTabStrip.this.onTabClick(i);
                if (BaseScrollTabStrip.this.tabClickListener != null) {
                    BaseScrollTabStrip.this.tabClickListener.onTabClick(i);
                }
            }
        });
        boolean z = this.fillTabBlank;
        view.setPadding(z ? 0 : this.tabPadding, 0, z ? 0 : this.tabPadding, 0);
        this.tabsContainer.addView(view, i, this.defaultTabLayoutParams);
    }

    public void scrollToChild(int i, int i2) {
        if (this.tabCount == 0) {
            return;
        }
        int left = this.tabsContainer.getChildAt(i).getLeft() + i2;
        if (i > 0 || i2 > 0) {
            left -= this.scrollOffset;
        }
        if (left != this.lastScrollX) {
            this.lastScrollX = left;
            smoothScrollTo(left, 0);
        }
        this.currentPosition = i;
    }

    public void switchTabTextColor(int i, int i2) {
        TextView textView = (TextView) this.tabsContainer.getChildAt(i);
        TextView textView2 = (TextView) this.tabsContainer.getChildAt(i2);
        if (Build.VERSION.SDK_INT >= 21) {
            if (textView != null) {
                ObjectAnimator.ofArgb(textView, "textColor", this.indicatorColor, this.tabTextColor).setDuration(200L).start();
            }
            if (textView2 == null) {
                return;
            }
            ObjectAnimator.ofArgb(textView2, "textColor", this.tabTextColor, this.indicatorColor).setDuration(200L).start();
            return;
        }
        if (textView != null) {
            textView.setTextColor(this.tabTextColor);
        }
        if (textView2 == null) {
            return;
        }
        textView2.setTextColor(this.indicatorColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int i;
        super.onDraw(canvas);
        if (isInEditMode() || this.tabCount == 0) {
            return;
        }
        int height = getHeight();
        this.rectPaint.setColor(this.indicatorColor);
        View childAt = this.tabsContainer.getChildAt(this.currentPosition);
        if (childAt != null) {
            float left = childAt.getLeft();
            float right = childAt.getRight();
            float f = this.currentPositionOffset;
            float f2 = 0.0f;
            if (f != 0.0f && (i = this.currentPosition) < this.tabCount - 1) {
                if (f > 0.0f) {
                    View childAt2 = this.tabsContainer.getChildAt(i + 1);
                    float left2 = childAt2 == null ? 0.0f : childAt2.getLeft();
                    if (childAt2 != null) {
                        f2 = childAt2.getRight();
                    }
                    float f3 = this.currentPositionOffset;
                    left = (left2 * f3) + ((1.0f - f3) * left);
                    right = (f2 * f3) + ((1.0f - f3) * right);
                } else {
                    View childAt3 = this.tabsContainer.getChildAt(i - 1);
                    float left3 = childAt3 == null ? 0.0f : childAt3.getLeft();
                    if (childAt3 != null) {
                        f2 = childAt3.getRight();
                    }
                    float f4 = this.currentPositionOffset;
                    left = ((-f4) * left3) + ((f4 + 1.0f) * left);
                    right = ((-f4) * f2) + ((f4 + 1.0f) * right);
                }
            }
            canvas.drawRect(left, height - this.indicatorHeight, right, height, this.rectPaint);
        }
        if (this.underlineHeight > 0) {
            this.rectPaint.setColor(this.underlineColor);
            canvas.drawRect(0.0f, height - this.underlineHeight, this.tabsContainer.getWidth(), height, this.rectPaint);
        }
        int i2 = 0;
        if (this.showDivider) {
            this.dividerPaint.setColor(this.dividerColor);
            for (int i3 = 0; i3 < this.tabCount - 1; i3++) {
                View childAt4 = this.tabsContainer.getChildAt(i3);
                canvas.drawLine(childAt4.getRight(), this.dividerPadding, childAt4.getRight(), height - this.dividerPadding, this.dividerPaint);
            }
        }
        if (this.redDotIndex == null) {
            return;
        }
        while (true) {
            boolean[] zArr = this.redDotIndex;
            if (i2 >= zArr.length) {
                return;
            }
            if (zArr[i2] && this.tabsContainer.getChildCount() > 0) {
                canvas.drawCircle(this.tabsContainer.getChildAt(i2).getLeft() + ((childAt.getWidth() * 8) / 10), childAt.getHeight() / 4, Density.dip2Px(getContext(), 5.0f), this.redDot);
            }
            i2++;
        }
    }

    public TabClickListener getTabClickListener() {
        return this.tabClickListener;
    }

    public void setTabClickListener(TabClickListener tabClickListener) {
        this.tabClickListener = tabClickListener;
    }

    public int[] getIcons() {
        return this.icons;
    }

    public void setIcons(int[] iArr) {
        this.icons = iArr;
        notifyAllTabs();
    }

    public String[] getTitles() {
        return this.titles;
    }

    public void setTitles(String[] strArr) {
        this.titles = strArr;
        notifyAllTabs();
    }

    public float getCurrentPositionOffset() {
        return this.currentPositionOffset;
    }

    public void setCurrentPositionOffset(float f) {
        this.currentPositionOffset = f;
    }

    @Override
    public void onRestoreInstanceState(Parcelable parcelable) {
        SavedState savedState = (SavedState) parcelable;
        super.onRestoreInstanceState(savedState.getSuperState());
        this.currentPosition = savedState.currentPosition;
        requestLayout();
    }

    @Override
    public Parcelable onSaveInstanceState() {
        SavedState savedState = new SavedState(super.onSaveInstanceState());
        savedState.currentPosition = this.currentPosition;
        return savedState;
    }

    public static class SavedState extends View.BaseSavedState {
        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
            @Override
            public SavedState createFromParcel(Parcel parcel) {
                return new SavedState(parcel);
            }

            @Override
            public SavedState[] newArray(int i) {
                return new SavedState[i];
            }
        };
        int currentPosition;

        public SavedState(Parcelable parcelable) {
            super(parcelable);
        }

        private SavedState(Parcel parcel) {
            super(parcel);
            this.currentPosition = parcel.readInt();
        }

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            super.writeToParcel(parcel, i);
            parcel.writeInt(this.currentPosition);
        }
    }
}